class Solution:
    def longestValidParentheses(self, s: str) -> int:
        n = len(s)
        if n == 0:
            return 0

        markList = [0] * n
        stack = []
        for i, v in enumerate(s):
            if v == '(':
                stack.append((v, i))
            else: # ')'
                if len(stack) == 0:
                    pass
                else:
                    markList[i] = 1
                    markList[stack.pop()[1]] = 1
        maxLen = 0
        curLen = 0
        for v in markList:
            if v == 1:
                curLen += 1
                maxLen = max(maxLen, curLen)
            else:
                maxLen = max(maxLen, curLen)
                curLen = 0
        return maxLen

solu = Solution()
print(solu.longestValidParentheses('(()'))